<?php
// $Id: PropertyType.class.php 73 2007-02-12 03:45:43Z tswicegood $
/**
 * This file contains {@link PropertyType} which is part of the PHP Content Repository 
 * (phpCR), a derivative of the Java Content Repository JSR-170, and is
 * licensed under the Apache License, Version 2.0.
 *
 * This file is based on the code created for
 * {@link http://www.jcp.org/en/jsr/detail?id=170 JSR-170}
 *
 * @author Travis Swicegood <travis@domain51.net>
 * @copyright PHP Code Copyright &copy; 2004-2005, Domain51, United States
 * @copyright Original Java and Documentation 
 *  Copyright &copy; 2002-2004, Day Management AG, Switerland
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
 *      Version 2.0
 * @package phpContentRepository
 */


/**
 * Load the {@link phpCR} library file(s)
 */ 
require_once(dirname(__FILE__) . '/phpCR.library.php');


/**
 * The property types supported by the phpCR standard.
 *
 * This interface defines following property types:
 * <ul>
 *    <li>{@link PropertyType::STRING}</li>
 *    <li>{@link PropertyType::BINARY}</li>
 *    <li>{@link PropertyType::LONG}</li>
 *    <li>{@link PropertyType::DOUBLE}</li>
 *    <li>{@link PropertyType::DATE}</li>
 *    <li>{@link PropertyType::BOOLEAN}</li>
 *    <li>{@link PropertyType::SOFTLINK}</li>
 *    <li>{@link PropertyType::REFERENCE}</li>
 * </ul>
 *
 * <b>PHP Note</b>: Some of these values do not translate from Java into
 * PHP.  Example: Java long = PHP int; Java double = PHP float.  In order to
 * emulate the JCR standard, however, I have left them.
 *
 * @author PHP - Travis Swicegood <travis@domain51.net>
 * @copyright Copyright &copy; 2004-2005, Domain51
 * @package phpContentRepository
 */
class PropertyType
{
   /**
    * The supported property types.
    */
    const STRING = 1;
    const BINARY = 2;
    const INT = 3;
    const LONG = self::INT;
    const FLOAT = 4;
    const DOUBLE = self::DOUBLE;
    const DATE = 5;
    const BOOLEAN = 6;
    const SOFTLINK = 7;
    const REFERENCE = 8;

   /**
    * Undefined type.
    */
    const UNDEFINED = 0;

   /**
    * The names of the supported property types,
    * as used in serialization.
    */
    const TYPENAME_STRING = "String";
    const TYPENAME_BINARY = "Binary";
    const TYPENAME_INT = "Integer";
    const TYPENAME_LONG = self::TYPENAME_INT;
    const TYPENAME_FLOAT = "Float";
    const TYPENAME_DOUBLE = self::TYPENAME_FLOAT;
    const TYPENAME_DATE = "Date";
    const TYPENAME_BOOLEAN = "Boolean";
    const TYPENAME_SOFTLINK = "SoftLink";
    const TYPENAME_REFERENCE = "Reference";

    /**
     * Returns the name of the specified type,
     * as used in serialization.
     * @param type the property type
     * @return the name of the specified type
     * @throws IllegalArgumentException if type
     * is not a valid property type.
     */
    static public function nameFromValue($type) 
    {
        assert('is_int($type)');
        
        switch ($type) {
        case PropertyType::STRING :
            return PropertyType::TYPENAME_STRING;
            break;
            
        case PropertyType::BINARY :
            return PropertyType::TYPENAME_BINARY;
            break;
            
        case PropertyType::BOOLEAN :
            return PropertyType::TYPENAME_BOOLEAN;
            break;
            
        case PropertyType::INT :
        case PropertyType::LONG :
            return PropertyType::TYPENAME_LONG;
            break;
            
        case PropertyType::FLOAT :
        case PropertyType::DOUBLE :
            return TYPENAME_FLOAT;
            break;
            
        case PropertyType::DATE :
            return PropertyType::TYPENAME_DATE;
            break;
            
        case PropertyType::SOFTLINK :
            return PropertyType::TYPENAME_SOFTLINK;
            break;
            
        case PropertyType::REFERENCE :
		    return PropertyType::TYPENAME_REFERENCE;
            break;
            
	    default:
	        throw new IllegalArgumentException("unknown type: " + type);
            break;
        }
    }
    
    
   /**
    * Returns the numeric constant value of the type with the specified name.
    *
    * @param string
    *   The name of the property type
    * @return int 
    *   The numeric constant value
    *
    * @throws {@link IllegalArgumentException}
    *   If $name is not a valid property type name.
    */
    static public function valueFromName($name) 
    {
        assert('is_string($name)');
        
        switch ($name) {
        case PropertyType::TYPENAME_STRING :
            return PropertyType::STRING;
            break;
            
        case PropertyType::TYPENAME_BINARY :
            return PropertyType::BINARY;
            break;
            
        case PropertyType::TYPENAME_BOOLEAN :
            return PropertyType::BOOLEAN;
            break;
            
        case PropertyType::TYPENAME_INT :
        case PropertyType::TYPENAME_LONG :
            return PropertyType::INT;
            break;
            
        case PropertyType::TYPENAME_FLOAT :
        case PropertyType::TYPENAME_DOUBLE :
            return PropertyType::FLOAT;
            break;
            
        case PropertyType::TYPENAME_DATE :
            return PropertyType::DATE;
            break;
            
        case PropertyType::TYPENAME_SOFTLINK :
            return PropertyType::SOFTLINK;
            break;
            
        case PropertyType::TYPENAME_REFERENCE :
            return PropertyType::REFERENCE;
            break;
            
        default :
            throw new IllegalArgumentException("Unknown type : " . $name);
            break;
        }
    }
    
    
   /**
    * Insures that this class does not get instantiated or sub-classed
    */
    final private function __construct()
    {
        
    }
    
}
